package ioperation;

import Book.Book;
import Book.Booklist;

import java.util.Scanner;

/**
 * Created with Interllij IDEA.
 * Descriptiption:
 * User: 王澄慧
 * Date: 2022-04-16
 * Time: 20:19
 */
public class Deleteiperation implements IPeration{
    @Override
    public void work(Booklist booklist) {
        System.out.println("删除图书");
        System.out.println("请输入你要删除图书的名字：");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int index = 0;
        int i = 0;
        int currentSize = booklist.getUserSize();
        for (; i < currentSize; i++) {
            Book book = booklist.getpos(i);
            if (book.getName().equals(name)) {
                index = i;
                break;
            }
        }
        if (i == currentSize) {
            System.out.println("没有你要删除的图书！");
            return;
        }
        for (int j = index; j < currentSize-1; j++) {
            Book book = booklist.getpos(j+1);
            booklist.setBooks(j,book);

        }
        booklist.setBooks(currentSize-1,null);
        booklist.setUserSize(currentSize-1);
        System.out.println("删除结束");
    }
    }
